﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using QuanLiThuVien.Source;
using QuanLiThuVien.LinkModel;

namespace QuanLiThuVien
{
	public partial class MuonSach
    {
        private InsertInfo m_insertInfo = new InsertInfo();
        private UpdateInfo m_updateInfo = new UpdateInfo();
        private DeleteInfo m_deleteInfo = new DeleteInfo();
        private SelectInfo m_selectInfo = new SelectInfo();
        private ShowInfo m_showInfo = new ShowInfo();

        private string m_sMaPhieuMuon;

        private THUVIEN.Service1 serv = new THUVIEN.Service1();

		public MuonSach()
		{
			this.InitializeComponent();
            dtmNgayMuon.Text = DateTime.Now.ToString();
            m_showInfo.ShowListPhieuMuon(dataMuonSach);
		}

        private void ClearIndexSach()
        {
            txtMSNamXuatBan.Text = "";
            txtMSNhaXuatBan.Text = "";
            txtMSTenSach.Text = "";
            txtMSTheLoai.Text = "";
        }

        private void ClearIndexDocGia()
        {
            txtMSHoTen.Text = "";
        }

        private void showThongTinSach()
        {
            string m_sMaSach = serv.SelectMaSach_CuonSach(txtMSMaCuonSach.Text);
            DSSach temp = new DSSach();

            temp.MaSach = serv.SelectThongTinSach_MaSach(m_sMaSach).MaSach.ToString();
            temp.MaDauSach = serv.SelectThongTinSach_MaSach(m_sMaSach).MaDauSach.ToString();
            temp.TenDauSach = serv.SelectThongTinSach_MaSach(m_sMaSach).TenDauSach.ToString();
            temp.TenTheLoai = serv.SelectThongTinSach_MaSach(m_sMaSach).TenTheLoai.ToString();
            temp.HoTenTacGia = serv.SelectThongTinSach_MaSach(m_sMaSach).HoTenTacGia.ToString();
            temp.NhaXuatBan = serv.SelectThongTinSach_MaSach(m_sMaSach).NhaXuatBan.ToString();
            temp.NamXuatBan = Int32.Parse(serv.SelectThongTinSach_MaSach(m_sMaSach).NamXuatBan.ToString());

            txtMSTenSach.Text = temp.TenDauSach;
            txtMSTheLoai.Text = temp.TenTheLoai;
            txtMSNhaXuatBan.Text = temp.NhaXuatBan;
            txtMSNamXuatBan.Text = temp.NamXuatBan.ToString();
        }

        private bool KiemTraMuonSachTrung(string MaDocGia, string MaSach)
        {
            for (int i = 0; i < serv.SelectMaSach_MuonSach(MaDocGia).Length; i++)
                if (MaSach == serv.SelectMaSach_MuonSach(MaDocGia)[i])
                    return true;
            return false;
        }

        private void txtMSMaCuonSach_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (serv.SelectMaSach_CuonSach(txtMSMaCuonSach.Text) != null)
            {
                showThongTinSach();
                if (serv.SelectTinhTrangCuonSach(txtMSMaCuonSach.Text) != 0)
                {
                    MessageBox.Show("Cuốn sách đã được cho mượn, chưa nhận về", "Thông báo");
                    ClearIndexSach();
                    txtMSMaCuonSach.Text = "";
                    txtMSMaCuonSach.Focus();
                }
            }
            else ClearIndexSach();
        }

        private void txtMSMaDocGia_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (serv.SelectTenDocGia(txtMSMaDocGia.Text) != null)
                txtMSHoTen.Text = serv.SelectTenDocGia(txtMSMaDocGia.Text);
            else ClearIndexDocGia();
        }

        private void bntXacNhan_Click(object sender, RoutedEventArgs e)
        {
            if (serv.SelectNgayHetHan_DocGia(txtMSMaDocGia.Text) < DateTime.Now)
                MessageBox.Show("Thẻ hết hạn! Không mượn được sách", "Thông báo");
            else
            {
                txtMSMaDocGia.IsEnabled = false;
                DateTime m_dNgayPhaiTra = dtmNgayMuon.DisplayDate.AddDays(serv.SelectGiaTriThamSo("NgayMuonToiDa"));
                m_sMaPhieuMuon = m_insertInfo.InsertPhieuMuon(txtMSMaDocGia.Text, DateTime.Parse(dtmNgayMuon.SelectedDate.ToString()), m_dNgayPhaiTra, 0);
                txtMSMaCuonSach.IsEnabled = true;
                bntMuonSach.IsEnabled = true;
                bntKetThuc.IsEnabled = true;
                bntXacNhan.IsEnabled = false;
            }
        }

        private void bntMuonSach_Click(object sender, RoutedEventArgs e)
        {
            int m_iSoSachDaMuon = serv.SelectSoSachMuon(m_sMaPhieuMuon);
            int m_iSoSachDangMuon_DocGia = serv.SelectSoSachDangMuon_DocGia(txtMSMaDocGia.Text);
            int m_iTinhTrangCuonSach = serv.SelectTinhTrangCuonSach(txtMSMaCuonSach.Text);
            if (serv.SelectMaSach_CuonSach(txtMSMaCuonSach.Text) != null)
            {
                if (m_iSoSachDangMuon_DocGia >= serv.SelectGiaTriThamSo("SachMuonToiDa"))
                {
                    MessageBox.Show("Độc giả đã mượn quá số sách quy định\nHãy trả sáng để được mượn thêm", "Thông báo");
                    ClearIndexSach();
                    txtMSMaCuonSach.Text = "";
                }
                else
                {
                    if (KiemTraMuonSachTrung(txtMSMaDocGia.Text, serv.SelectMaSach_CuonSach(txtMSMaCuonSach.Text)))
                    {
                        MessageBox.Show("Độc giả đã mượn sách giống như vầy rồi!", "Thông báo");
                        ClearIndexSach();
                        txtMSMaCuonSach.Text = "";
                    }
                    else if (m_iTinhTrangCuonSach == 1 || m_iTinhTrangCuonSach == 2 || m_iTinhTrangCuonSach == 3)
                    {
                        if (m_iTinhTrangCuonSach == 1)
                            MessageBox.Show("Lỗi: Cuốn sách đã có độc giả khác mượn!", "Thông báo");
                        else if (m_iTinhTrangCuonSach == 2)
                            MessageBox.Show("Lỗi: Cuốn sách đã bị hư hỏng!", "Thông báo");
                        else if (m_iTinhTrangCuonSach == 3)
                            MessageBox.Show("Lỗi: Cuốn sách đã mất!", "Thông báo");
                        ClearIndexSach();
                        txtMSMaCuonSach.Text = "";
                    }
                    else
                    {
                        m_insertInfo.InsertCTPhieuMuon(txtMSMaCuonSach.Text, m_sMaPhieuMuon, 1);
                        m_updateInfo.UpdateTinhTrangCuonSach(txtMSMaCuonSach.Text, 1);
                        m_updateInfo.UpdateSoSachMuon(m_sMaPhieuMuon, m_iSoSachDaMuon + 1);
                        m_updateInfo.UpdateSoSachDangMuon_DocGia(txtMSMaDocGia.Text, m_iSoSachDangMuon_DocGia + 1);
                        int m_iSoSachTrongKhoHienTai = serv.SelectSoLuong_Sach(serv.SelectMaSach_CuonSach(txtMSMaCuonSach.Text));
                        m_updateInfo.UpdateSoLuong_Sach(serv.SelectMaSach_CuonSach(txtMSMaCuonSach.Text), m_iSoSachTrongKhoHienTai - 1);
                        ClearIndexSach();
                        txtMSMaCuonSach.Text = "";
                        txtMSMaCuonSach.Focus();
                        m_showInfo.ShowListPhieuMuon(dataMuonSach);
                    }
                }
            }
            else MessageBox.Show("Kiểm tra lại mã cuốn sách", "Thông báo");
        }

        private void bntTimPhieuMuon_Click(object sender, RoutedEventArgs e)
        {
            dataMuonSach.ItemsSource = null;
            m_selectInfo.SelectPhieuMuon_MaDocGia(dataMuonSach, txtMSMaDocGia.Text);
        }
        
        private void bntKetThuc_Click(object sender, RoutedEventArgs e)
        {
            int m_iSoSachMuon = serv.SelectSoSachMuon(m_sMaPhieuMuon);
            if (m_iSoSachMuon == 0)
                m_deleteInfo.DeletePhieuMuon(m_sMaPhieuMuon);
            ClearIndexDocGia();
            ClearIndexSach();
            txtMSMaDocGia.Text = "";
            txtMSMaCuonSach.Text = "";
            txtMSMaCuonSach.IsEnabled = false;
            txtMSMaDocGia.IsEnabled = true;
            txtMSMaDocGia.Focus();
            bntMuonSach.IsEnabled = false;
            bntXacNhan.IsEnabled = true;
            bntKetThuc.IsEnabled = false;
        }
	}
}